package azcat.planner.gui;

import java.awt.EventQueue;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.Calendar;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class NuevoProyecto extends JFrame {
	private JPanel panel;
	private JButton btnNewButton;
	private JPanel centerPanel;
	private JLabel lblNoDeParte;
	private JTextField txtPartNumber;
	private JLabel lblNewLabel;
	private JComboBox comboBoxMonth;
	private JComboBox comboBoxDay;
	private JComboBox comboBoxYear;
	private MainWindow mw;
	private JButton btnCancelar;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//					NuevoProyecto frame = new NuevoProyecto();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}
	private ComboBoxModel<Integer> getDaysForMonths(int i){
		ComboBoxModel<Integer>[] sizes = new ComboBoxModel[4];
		sizes[0] = new DefaultComboBoxModel<Integer>(new Integer[] {});
		sizes[1] =  new DefaultComboBoxModel<Integer>(new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31});
		sizes[2] =  new DefaultComboBoxModel<Integer>(new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30});
		sizes[3] =  new DefaultComboBoxModel<Integer>(new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28});
		int month = i % 2;
		if(i == 2){
			return sizes[3];
		}
		else if(i == 8){
			return sizes[1];
		}
		else if(i == 0){
			return sizes[0];			
		}
		else if( month == 0){
			return sizes[2];
		}
		else{
			return sizes[1];
		}
		
	}

	/**
	 * Create the frame.
	 */
	public NuevoProyecto(MainWindow mw1) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				mw.enableMaterialsButton();
			}
		});
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.mw =mw1;
		mw.disableMaterialButton();
		
		setResizable(false);
		setBounds(100, 100, 450, 300);
		
		panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		btnNewButton = new JButton("Aceptar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(checkValues()){
					String deadline = comboBoxMonth.getSelectedItem() +"/"+ comboBoxDay.getSelectedItem()+"/"+comboBoxYear.getSelectedItem();
					mw.setProjectValues(txtPartNumber.getText(), deadline);
					mw.clearAll();
					mw.enableMaterialbutton();
					dispose();
				}
			}
		});
		panel.add(btnNewButton);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mw.enableMaterialbutton();
				dispose();
			}
		});
		panel.add(btnCancelar);
		
		centerPanel = new JPanel();
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new MigLayout("", "[][][][][][]", "[29.00][22.00][29.00]"));
		
		lblNoDeParte = new JLabel("No. de Parte:");
		lblNoDeParte.setFont(new Font("Tahoma", Font.BOLD, 13));
		centerPanel.add(lblNoDeParte, "cell 0 0,alignx left");
		
		txtPartNumber = new JTextField();
		centerPanel.add(txtPartNumber, "cell 1 0 5 1,alignx left");
		txtPartNumber.setColumns(20);
		
		lblNewLabel = new JLabel("Fecha de Entrega:");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		centerPanel.add(lblNewLabel, "cell 0 2");
		
		comboBoxMonth = new JComboBox();
		comboBoxMonth.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				comboBoxDay.setModel(getDaysForMonths(comboBoxMonth.getSelectedIndex()));
			}
		});
		comboBoxMonth.setModel(new DefaultComboBoxModel(new String[] {"Mes","01","02","03","04","05","06","07","08","09","10","11","12"}));
		centerPanel.add(comboBoxMonth, "cell 2 2,growx");
		
		comboBoxDay = new JComboBox();
		centerPanel.add(comboBoxDay, "cell 3 2,growx");
		
		comboBoxYear = new JComboBox();
		comboBoxYear.setModel(new DefaultComboBoxModel(generateYears()));
		centerPanel.add(comboBoxYear, "cell 4 2,growx");

	}
	
	private boolean checkValues(){
		if(comboBoxMonth.getSelectedIndex() == 0){
			JOptionPane.showMessageDialog(this,
				    "Ingresar Fecha Valida",
				    "Error",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(txtPartNumber.getText().equals("")){
			JOptionPane.showMessageDialog(this,
				    "Ingresar Numero de Parte",
				    "Error",
				    JOptionPane.ERROR_MESSAGE);
			txtPartNumber.requestFocus();
			return false;
		}
		else{
			return true;
		}
	}
	
	private String [] generateYears(){
		Calendar c = Calendar.getInstance();
		String []years = new String[10];
		for(int i= 0; i< 10; i++){
			years[i] = c.get(Calendar.YEAR)+i+"";
		}
		return years;
	}

}
